package br.ufrj.ppgi.jemf_advanced_example.task;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Task;
import br.ufrj.ppgi.jemf.mobile.view.TaskView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Task detail screen.
 * This fragment is either contained in a {@link TaskListActivity} in two-pane mode (on tablets) or a {@link TaskDetailActivity} on handsets.
 */
public class TaskDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_TASK = "activated_task";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "TASK";
    /**
     * The Task content this fragment is showing.
     */
    private static Task task;
    /**
     * The Task view holder.
     */
    private static TaskView taskView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public TaskDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Task content specified by the fragment arguments.
        if (getArguments().containsKey(TaskDetailFragment.ARG_ID)) {
        	task = getArguments().getParcelable(TaskDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Task content
	    if (task != null) {
	    	taskView = new TaskView(getActivity(), false);
	    	// Title
	    	taskView.setTaskTitleText(task.getTitle());
	    	// Description
	    	taskView.setTaskDescriptionText(task.getDescription());
	    	// Status
	    	taskView.setTaskStatusText(task.getStatusString());
	    	// Start Date
	    	taskView.setTaskStartDateText(task.getStartDateString());
	    	// End Date
	    	taskView.setTaskEndDateText(task.getEndDateString());
	    	// Priority
	    	taskView.setTaskPriorityText(task.getPriorityString());

	    	// Its necessary to add the TaskView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Task View as a child of the ScrollView.
	    	scrollView.addView(taskView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Task
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_TASK)) {
        	task = savedInstanceState.getParcelable(STATE_TASK);
        }
	}





	/**
     * Get updated Task
     * @return Task
     */
	public static Task getTask() {
		if (BuildConfig.DEBUG) Log.i(TaskListActivity.TAG, "TaskDetailFragment ::: getTask");
    	return task;
    }

	/**
	 * Update Task data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((task != null) && (taskView != null)) {
    		if (BuildConfig.DEBUG) Log.i(TaskListActivity.TAG, "TaskDetailFragment ::: updateDataFromUI");
    		// Update Task object with new data included by user through components
    		task.setTitle(taskView.getTaskTitleText());
    		task.setDescription(taskView.getTaskDescriptionText());
    		task.setStatusString(taskView.getTaskStatusText());
    		task.setStartDateString(taskView.getTaskStartDateText());
    		task.setEndDateString(taskView.getTaskEndDateText());
    		task.setPriorityString(taskView.getTaskPriorityText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Task.
        outState.putParcelable(STATE_TASK, task);
    }
}
